#!/bin/bash
#
# The main menu for building MaxWit Linux
#
# http://www.maxwit.com
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


MW_TOP_DIR=`dirname $0`
cd ${MW_TOP_DIR}
MW_TOP_DIR=${PWD}


echo ${PWD} | grep "^${HOME}" > /dev/null 2>&1 || \
{
	echo "MaxWit must be built under \$HOME dir!"
	exit 1
}

. core/bmw_menu
. core/pkg_list
. core/bmw_base

. host/build
. toolchain/build
. kernel/build
. application/build


BUILD_JOBS="-j2"
SB2_CMD="sb2 -m install"

MM_FILE="/proc/sys/vm/mmap_min_addr"
MM_MIN_ADDR=`cat ${MM_FILE}` || exit 1
if [ "${MM_MIN_ADDR}" != "0" ]; then
	if [ ${USER} = root ]; then
		echo 0 > ${MM_FILE} || exit 1
	else
		echo "Pls check \"${MM_FILE}\"!"
		exit 1
	fi
fi

PROG_NAME=`basename $0`

mkdir -vp ${APP_BUILD_PATH} 
mkdir -vp ${KERNEL_BUILD_PATH} 
mkdir -vp ${UTILS_BUILD_PATH}

if [ "${PROG_NAME}" == "build-all" ]; then
#	echo -n "Removing old dirs ."
#	for dir in ${SYSROOT_PATH} ${ROOTFS_PATH} ${BUILD_PATH} ${UTILS_PREFIX} ${IMAGES_PATH}
#	do
#		rm -rf $dir
#		echo -n "."
#		mkdir -p $dir
#	done
#
#	for dir in toolchain host application
#	do
#		mkdir -p ${BUILD_PATH}/$dir
#	done
#
#	echo " OK."

	BuildUtils || exit 1

	BuildToolchain || exit	1

	BuildKernel	|| exit 1

	BuildSysCore1 || exit 1

	BuildAppList ${MWP_PERL}	\
		${MWP_VIM}	\
		${MWP_ALSA_UTILS} \
		${MWP_ALSA_LIB}	\
		${MWP_MADPLAY} \
		${MWP_LIBMAD} \
		${MWP_MPG123} \
		${MWP_LIBID3TAG} \
		${MWP_FAAD2} \
		${MWP_MPLAYER}	\
		${MWP_FREETYPE}	\
		${MWP_JPEG}	\
		${MWP_LIBUNGIF}	\
		${MWP_TIFF}	\
		${MWP_LIBPNG} \
		${MWP_FBV}	\
		${MWP_SDL}	\
		${MWP_SDL_IMAGE} \
		${MWP_SDL_TTF} \
		${MWP_SDL_MIXER} \
		${MWP_DFB}	\
		${MWP_DFB_EX}	\
		${MWP_USB_UTILS} \
		${MWP_TSLIB} \
		${MWP_PRBOOM} \
		||	exit 1

		. ${MW_TOP_DIR}/${MWP_PRBOOM}/setup.sh

	RunQemu

	exit 0
fi



while true
do
	ShowMenu "[MaxWit Linux Building Menu] (configured for ${TARGET_SOC})" \
			"Build Host Environment" \
			"Build GNU Toolchain" \
			"Build Bootloader (g-bios)" \
			"Build Linux Kernel (${MWP_KERNEL})" \
			"Build Basic System (busybox or coreutils)" \
			"Build Lib/App (vim,perl)" \
			"Build Lib/App (alsa,libmad,mpg123,madplay)" \
			"Build Lib/App (MPlayer)" \
			"Build Lib/App (jpeg,gif,tiff,png,fbv)" \
			"Build Lib/App (SDL,DirectFB)" \
			"Build Lib/App (usbutils,tslib)" \
			"Build 3D Game (doom)" \
			"Testing on QEMU" \
			"Create File System Images (yaffs2,jffs2,cramfs,etc.)"


	case $? in
	0) # fixme
		exit 1
		;;

	1)  BuildUtils || exit 1
	    ;;

	2)  BuildToolchain || exit 1
	    ;;

	3)  cd ${MW_TOP_DIR}/g-bios
	    ./configure
	    make || exit 1
	    ;;

	4)  BuildKernel || exit 1
		;;

	    # or BuildSysCore2
	5)  BuildSysCore1 || exit 1
		;;

	6)  BuildAppList ${MWP_PERL} \
				${MWP_VIM} \
		|| exit 1

#		${MWP_DEBIANUTILS} \
#		${MWP_E2FS_LIBS} \
#		${MWP_E2FS}
	    ;;

	7)  BuildAppList ${MWP_ALSA_UTILS} \
				${MWP_ALSA_LIB} \
				${MWP_MADPLAY} \
				${MWP_LIBMAD} \
				${MWP_MPG123} \
				${MWP_LIBID3TAG} \
		|| exit 1
		;;

	8)  BuildAppList ${MWP_FAAD2} \
				${MWP_MPLAYER} \
				${MWP_FREETYPE}  \
		|| exit 1
		;;

	9)  BuildAppList ${MWP_JPEG} \
				${MWP_LIBUNGIF} \
				${MWP_TIFF} \
				${MWP_LIBPNG} \
				${MWP_FBV} \
		|| exit 1
		;;

	10) BuildAppList ${MWP_SDL} \
				${MWP_SDL_IMAGE} \
				${MWP_SDL_TTF} \
				${MWP_SDL_MIXER} \
				${MWP_DFB} \
				${MWP_DFB_EX} \
		|| exit 1
		;;

	11) BuildAppList ${MWP_USB_UTILS} ${MWP_TSLIB} \
		|| exit 1
		;;

	12) BuildAppList ${MWP_PRBOOM} || exit 1
		. ${MW_TOP_DIR}/application/${MWP_PRBOOM}/setup.sh
		;;

	13) RunQemu
		;;

	14) BuildImage
		;;

	*)
		echo "Wrong Choice! Please Try Again."
		;;
	esac

	echo
done


test "${MM_MIN_ADDR}" != "0" && test ${USER} = root && \
	echo "${MM_MIN_ADDR}" > ${MM_FILE}

echo

